
package com.shop.cloud.mall.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.mall.common.entity.Supplier;
import com.shop.cloud.mall.admin.service.SupplierService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 供应商
 *
 * @author
 * @date 2023-04-20 10:25:10
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/supplier")
@Tag(description = "supplier", name = "供应商管理")
public class SupplierController {

    private final SupplierService supplierService;

	/**
	 * list查询
	 * @param supplier
	 * @return
	 */
	@Operation(summary = "list查询")
	@GetMapping("/list")
	public R getList(Supplier supplier) {
		return R.ok(supplierService.list(Wrappers.query(supplier).lambda()
				.select(Supplier::getId,
						Supplier::getName)));
	}

    /**
     * 供应商分页列表
     * @param page 分页对象
     * @param supplier 供应商
     * @return
     */
    @Operation(summary = "供应商分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('mall:supplier:index')")
    public R getPage(Page page, Supplier supplier) {
        return R.ok(supplierService.page(page, Wrappers.query(supplier)));
    }

    /**
     * 供应商查询
     * @param id
     * @return R
     */
    @Operation(summary = "供应商查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:supplier:get','mall:orderinfo:edit')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(supplierService.getById(id));
    }

    /**
     * 供应商新增
     * @param supplier 供应商
     * @return R
     */
    @Operation(summary = "供应商新增")
    @SysLog("新增供应商")
    @PostMapping
    @PreAuthorize("@ato.hasAuthority('mall:supplier:add')")
    public R save(@RequestBody Supplier supplier) {
        return R.ok(supplierService.save(supplier));
    }

    /**
     * 供应商修改
     * @param supplier 供应商
     * @return R
     */
    @Operation(summary = "供应商修改")
    @SysLog("修改供应商")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('mall:supplier:edit')")
    public R updateById(@RequestBody Supplier supplier) {
        return R.ok(supplierService.updateById(supplier));
    }

    /**
     * 供应商删除
     * @param id
     * @return R
     */
    @Operation(summary = "供应商删除")
    @SysLog("删除供应商")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('mall:supplier:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(supplierService.removeById(id));
    }

	/**
	 * 数量查询
	 * @param supplier 供应商
	 * @return
	 */
	@Operation(summary = "数量查询")
	@GetMapping("/count")
	@PreAuthorize("@ato.hasAuthority('mall:supplier:index')")
	public R getCount(Supplier supplier) {
		long count = supplierService.count(Wrappers.query(supplier));
		return R.ok(count);
	}
}
